Skip to content

Instantly share code, notes, and snippets.

@tessguefen
Created May 17, 2018 21:04
Show Gist options
  • Save tessguefen/ff3c4e96ac09158e5a8b0ae854934619 to your computer and use it in GitHub Desktop.
Save tessguefen/ff3c4e96ac09158e5a8b0ae854934619 to your computer and use it in GitHub Desktop.
Yotpo Purchase API
<mvt:if expr="g.Module_Function NE 'TokenList_ViewAll_Page'">
<mvt:comment>
|
| Send Purchases to Yotpo
| client_id = yotpo KEY
| client_secret = yotpo Secret
|
| There are 2 mvtcalls: 1 for the Access Token, and another for sending the data through with that token.
|
</mvt:comment>
<mvt:assign name="l.dbl_quotes" value="asciichar( 34 )" />
<mvt:assign name="l.yotpo:client_id" value="'YOUR CLIENT ID HERE'" />
<mvt:assign name="l.yotpo:client_secret" value="'YOUR CLIENT SECRET HERE'" />
<mvt:assign name="g.yotpopayload" value="'{' $
l.dbl_quotes $ 'client_id' $ l.dbl_quotes $ ':' $ l.dbl_quotes $ l.yotpo:client_id $ l.dbl_quotes $ ',' $
l.dbl_quotes $ 'client_secret' $ l.dbl_quotes $ ':' $ l.dbl_quotes $ l.yotpo:client_secret $ l.dbl_quotes $ ',' $
l.dbl_quotes $ 'grant_type' $ l.dbl_quotes $ ':' $ l.dbl_quotes $ 'client_credentials' $ l.dbl_quotes $ '}'" />
<mvt:call action="'https://api.yotpo.com/oauth/token'" method="'RAW'" content-type="'application/json'" fields="'yotpopayload'">
<mvt:assign name="l.void" value="miva_json_decode( s.callvalue, l.yotpo_return )" />
</mvt:call>
<mvt:if expr="NOT ISNULL l.yotpo_return:access_token">
<mvt:comment> Access Token Granted </mvt:comment>
<mvt:assign name="g.yotpo_purchase_payload" value="'{'" />
<mvt:assign name="l.yotpo_order_date" value="time_t_year( l.settings:order:orderdate, g.Merchant_Local_Timezone ) $ '-' $ padl( time_t_month( l.settings:order:orderdate, g.Merchant_Local_Timezone ), 2, 0 ) $ '-' $ padl( time_t_dayofmonth( l.settings:order:orderdate, g.Merchant_Local_Timezone ), 2, 0 )" />
<mvt:assign name="g.yotpo_purchase_payload" value="g.yotpo_purchase_payload $
l.dbl_quotes $ 'validate_data' $ l.dbl_quotes $ ':true,' $
l.dbl_quotes $ 'platform' $ l.dbl_quotes $ ':' $ l.dbl_quotes $ 'miva' $ l.dbl_quotes $ ',' $
l.dbl_quotes $ 'utoken' $ l.dbl_quotes $ ':' $ l.dbl_quotes $ encodejavascriptstring( l.yotpo_return:access_token ) $ l.dbl_quotes $ ',' $
l.dbl_quotes $ 'email' $ l.dbl_quotes $ ':' $ l.dbl_quotes $ encodejavascriptstring( l.settings:order:bill_email ) $ l.dbl_quotes $ ',' $
l.dbl_quotes $ 'customer_name' $ l.dbl_quotes $ ':' $ l.dbl_quotes $ encodejavascriptstring( l.settings:order:bill_fname ) $ l.dbl_quotes $ ',' $
l.dbl_quotes $ 'order_id' $ l.dbl_quotes $ ':' $ l.dbl_quotes $ encodejavascriptstring( l.settings:order:id ) $ l.dbl_quotes $ ',' $
l.dbl_quotes $ 'order_date' $ l.dbl_quotes $ ':' $ l.dbl_quotes $ encodejavascriptstring( l.yotpo_order_date ) $ l.dbl_quotes $ ',' $
l.dbl_quotes $ 'currency_iso' $ l.dbl_quotes $ ':' $ l.dbl_quotes $ 'USD' $ l.dbl_quotes $ ',' $
l.dbl_quotes $ 'products' $ l.dbl_quotes $ ': {'" />
<mvt:comment> Begin Products Loop </mvt:comment>
<mvt:foreach iterator="group" array="order:groups">
<mvt:if expr="POS1 GT 1">
<mvt:assign name="g.yotpo_purchase_payload" value="g.yotpo_purchase_payload $ ','" />
</mvt:if>
<mvt:assign name="l.yotpo_img" value="NULL" />
<mvt:if expr="l.settings:group:imagetypes:main">
<mvt:assign name="l.yotpo_img" value="g.basehref $ l.settings:group:imagetypes:main" />
</mvt:if>
<mvt:if expr="ISNULL l.settings:group:link">
<mvt:do file="g.Module_Feature_URI_UT" name="l.settings:group:link" value="Store_Product_URL( l.settings:group:product, l.flags )" />
</mvt:if>
<mvt:assign name="g.yotpo_purchase_payload" value="g.yotpo_purchase_payload $
l.dbl_quotes $ encodejavascriptstring( l.settings:group:code ) $ l.dbl_quotes $ ': {' $
l.dbl_quotes $ 'url' $ l.dbl_quotes $ ':' $ l.dbl_quotes $ encodejavascriptstring( l.settings:group:link ) $ l.dbl_quotes $ ',' $
l.dbl_quotes $ 'name' $ l.dbl_quotes $ ':' $ l.dbl_quotes $ encodejavascriptstring( l.settings:group:name ) $ l.dbl_quotes $ ',' $
l.dbl_quotes $ 'image' $ l.dbl_quotes $ ':' $ l.dbl_quotes $ encodejavascriptstring( l.yotpo_img ) $ l.dbl_quotes $ ',' $
l.dbl_quotes $ 'description' $ l.dbl_quotes $ ':' $ l.dbl_quotes $ encodejavascriptstring( l.settings:group:product:descrip ) $ l.dbl_quotes $ ',' $
l.dbl_quotes $ 'price' $ l.dbl_quotes $ ':' $ l.dbl_quotes $ encodejavascriptstring( l.settings:group:price ) $ l.dbl_quotes $ ',' $
l.dbl_quotes $ 'specs' $ l.dbl_quotes $ ': {' $
l.dbl_quotes $ 'upc' $ l.dbl_quotes $ ':' $ l.dbl_quotes $ encodejavascriptstring( l.settings:group:product:sku ) $ l.dbl_quotes $
'}' $
'}'" />
</mvt:foreach>
<mvt:comment> End Products Loop </mvt:comment>
<mvt:assign name="g.yotpo_purchase_payload" value="g.yotpo_purchase_payload $ '}}'" />
<mvt:comment>
Ping Yotpo with the purchase
</mvt:comment>
<mvt:call action="'https://api.yotpo.com/apps/' $ l.yotpo:client_id $ '/purchases/'" method="'RAW'" content-type="'application/json'" fields="'yotpo_purchase_payload'">
<mvt:assign name="l.void" value="miva_json_decode( s.callvalue, l.yotpo_purchase_return )" />
</mvt:call>
</mvt:if>
</mvt:if>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment